/**
* Appcelerator Titanium Platform
* Copyright (c) 2009-2011 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
**/
// Code is stripped-down version of Tweetanium, to expose new structure paradigm

var S = {};

(function(){
	S.app = {};
	S.Xhr = {};
	S.json = {};
	
	var empty = {};
	function mixin(/*Object*/ target, /*Object*/ source){
		var name, s, i;
		for(name in source){
			s = source[name];
			if(!(name in target) || (target[name] !== s && (!(name in empty) || empty[name] !== s))){
				target[name] = s;
			}
		}
		return target; // Object
	};
	S.mixin = function(/*Object*/ obj, /*Object...*/ props){
		if(!obj){ obj = {}; }
		for(var i=1, l=arguments.length; i<l; i++){
			mixin(obj, arguments[i]);
		}
		return obj; // Object
	};
	
	//create a new object, combining the properties of the passed objects with the last arguments having
	//priority over the first ones
	S.combine = function(/*Object*/ obj, /*Object...*/ props) {
		var newObj = {};
		for(var i=0, l=arguments.length; i<l; i++){
			mixin(newObj, arguments[i]);
		}
		return newObj;
	};

	/*
		Branching logic based on OS
	*/
	var osname = Ti.Platform.osname;
	S.os = function(/*Object*/ map) {
		var def = map.def||null; //default function or value
		if (typeof map[osname] != 'undefined') {
			if (typeof map[osname] == 'function') { return map[osname](); }
			else { return map[osname]; }
		}
		else {
			if (typeof def == 'function') { return def(); }
			else { return def; }
		}
	};
	
	/*
	 * XHR handler
	 */
	var xhr = Ti.Network.createHTTPClient({timeout:1500});
	S.Xhr = function(_args) {
		Ti.API.debug("Universal XHR Call: " + _args.url);
		Ti.API.debug("XHR Params: " + JSON.stringify(_args.params));
		S.xhr = xhr;
		S.xhr.onload = _args.onload;
		S.xhr.onerror = _args.onerror;
		S.xhr.open(_args.method, _args.url);
		S.xhr.send(_args.params);
	};

	/*
	 * 	Generates time difference (i.e. 2 days ago)
	 */
	S.timeDifference = function(current, previous) {
	    var msPerMinute = 60 * 1000;
	    var msPerHour = msPerMinute * 60;
	    var msPerDay = msPerHour * 24;
	    var msPerMonth = msPerDay * 30;
	    var msPerYear = msPerDay * 365;
	    var elapsed = current - previous;

	    if (elapsed < msPerMinute) {
	         return Math.round(elapsed/1000) + ' seconds ago';   
	    } else if (elapsed < msPerHour) {
	         return Math.round(elapsed/msPerMinute) + ' minutes ago';   
	    } else if (elapsed < msPerDay ) {
	         return Math.round(elapsed/msPerHour ) + ' hours ago';   
	    } else if (elapsed < msPerMonth) {
	        return Math.round(elapsed/msPerDay) + ' days ago';   
	    } else if (elapsed < msPerYear) {
	        return Math.round(elapsed/msPerMonth) + ' months ago';   
	    } else {
	        return Math.round(elapsed/msPerYear ) + ' years ago';   
	    }
	}
	/*
	 * 	Add swipe functionality for Android
	 */
	S.isSwiping = false;
	S.makeSwipeable = function(view, allowVertical, tolerance) {
	    tolerance = tolerance || 4;
	    var start;
	    view.addEventListener('touchstart', function(evt) {
	        start = evt;
	    });
	    view.addEventListener('touchend', function (end) {
	    	S.isSwiping = false;
	        var dx = end.x - start.x, dy = end.y - start.y;
	        var dist = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
	        // only trigger if dragged further than 50 pixels
	        if (dist < 50) {
	            return;
	        }
	        var isVertical = Math.abs(dx / dy) < 1 / tolerance;
	        var isHorizontal = Math.abs(dy / dx) < 1 / tolerance;
	        // only trigger if dragged in a particular direction
	        if (!isVertical && !isHorizontal) {
	            return;
	        }
	        // disallow vertical swipe, depending on the setting
	        if (!allowVertical && isVertical) {
	            return;
	        }
	        // now fire the event off so regular 'swipe' handlers can use this!
	        end.direction = isHorizontal ? ((dx < 0) ? 'left' : 'right') : ((dy < 0) ? 'up' : 'down');
	        end.type = 'swipe';
	        view.fireEvent('swipe', end);
	    });
	}
	
			
}());

Ti.include("/struct/ui/ui.js");